﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Data;
namespace WPFExcelReport
{
    public partial class TLSMUL
    {

        public TLSMUL()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_MULLANG();
            DataContext = MV;
            InitializeComponent();
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsLoaded) return;
           
            base.Window_Loaded(sender, e);
        }
 

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = ""; 
            }
            else
            {
                KEY = TMV.MLLOC; 
            }
        }
        public override void OnEnOnly()
        {
            return;
            if (IsEnOnly)
            {
                filter = "EN='EN'";
                cvMaster.CustomFilter = filter;
            }
            else
            {
                cvMaster.CustomFilter = null;
            }
            base.OnEnOnly();
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {

            try
            {
                if (dwMaster == null)
                    return;
                dwData = TBMain.NewRow();
                dwData.ItemArray = dwMaster.ItemArray;
                //LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "MULLANG", 1);
                dwData["ROWNID"] = "0";
                TMV.InsertICC_MULLANG(dwData, app.company);
                TBMain.Rows.InsertAt(dwData, 0);
                vwMaster.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            if(KRS=="")
            {
                MessageBox.Show("先输入目标KEY");
                return;
            }
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["MLKEY"] =KRS;
            dwData["SCN"] = KRS;
            dwData["EN"] = "EN";
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertICC_MULLANG(dwData);
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        }
        private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter
                || e.Key != System.Windows.Input.Key.Return)
            {
                return;
            }
            if (dwMaster == null)
                return;
            this.Focus();
            TextBox txt = sender as TextBox;
            TMV.UMUL02(dwMaster);
            string key = TMV.MLKEY;
            string en = TMV.EN;
            filter = "MLKEY='" + key + "'";
            rows = TBMain.Select(filter);
            foreach(DataRow row in rows )
            {
                row["EN"] = en;
            } 
            vwMaster.View.MoveCurrentToNext();
            txt.Focus();
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                filter = "";
                dwData = TBMain.NewRow();
                dwData["MANDANT"] = app.Mandant;
                if (IsEnOnly)
                {
                    dwData["EN"] = " AND EN='EN'";
                    filter = "EN='EN'";
                }
                dwData["MLLOC"] = " AND MLLOC LIKE '" + ART + "%'";
                SResult = TMV.QMUL9999(dwData);
                MergeData(SResult, true);
                //cvMaster.CustomFilter = filter;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        string TType = "COMPANY";
        private void DiscoveryCommand(object sender, RoutedEventArgs e)
        {
            TType = "COMPANY";
            bgWorker.RunWorkerAsync();
        }
        private void BTDiscoveryCommand(object sender, RoutedEventArgs e)
        {
            TType = "TABLE";
            bgWorker.RunWorkerAsync();
        }
        public override void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            base.bgWorker_RunWorkerCompleted(sender, e);
            vwMaster.View.MoveCurrentTo(-1);
        }

        public override void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            EventDispatcher.Instance.RaiseStatus("开始更新", StatusEventType.StartProgress);
            switch (TType)
            {
                case "TABLE":
                    TMV.GetAllText2Translate("TABLE");
                    break;
                case "COMPANY":
                    TMV.GetAllText2Translate(app.company);
                    break;
            } 
        }
    }
}
